package com.swt.dao.impl.S_impl;

import com.swt.dao.S_dao.GetInfoDao;
import com.swt.domain.Aboutcourse;
import com.swt.domain.Course;
import com.swt.util.GetCurrentTime;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository(value = "S_getInfoDao")
public class GetInfoDaoImpl implements GetInfoDao {

    /*注入hibernateTemplate*/
    @Resource(name = "hibernateTemplate")
    private HibernateTemplate hibernateTemplate;
    @Override
    public List<Aboutcourse> getInfo(int clubid, int userid) {
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Aboutcourse.class);
        detachedCriteria.add(Restrictions.eq("aboutcourseUserid",userid));
        detachedCriteria.add(Restrictions.eq("aboutcourseClubid",clubid));
        //获取当前时间
        String today = GetCurrentTime.getToday();
        /*添加时间条件 只查询今天之后的*/
        detachedCriteria.add(Restrictions.ge("aboutcourseDate",today));
        List<Aboutcourse> list = (List<Aboutcourse>)hibernateTemplate.findByCriteria(detachedCriteria);
        return list;
    }

    @Override
    public void deleteInfo(Aboutcourse aboutcourse) {
        /*删除*/
        hibernateTemplate.delete(aboutcourse);
    }

    @Override
    public Aboutcourse getInfoById(int aboutcourseId) {
        Aboutcourse aboutcourse = hibernateTemplate.get(Aboutcourse.class, aboutcourseId);
        return aboutcourse;
    }

    @Override
    public Course getCourse(int courseid) {
        Course course = hibernateTemplate.get(Course.class, courseid);
        return course;
    }

    @Override
    public void saveCourse(Course course) {
        hibernateTemplate.save(course);
    }

}
